package com.leadingsoft.pwxk.commons;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.leadingsoft.pwxk.dto.FileInfoDto;
import com.leadingsoft.pwxk.enums.InterfaceEnum;
import com.leadingsoft.pwxk.utils.AesUtilTest;
import com.leadingsoft.pwxk.utils.ConnectUtil;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Getter
@Setter
@Slf4j
public class DealDataResult<T> {

	private static String sourceUrl = "http://10.102.33.36:8080";
	private static String agentUrl = "http://10.194.21.123:8080/data";

	public List<T> listDataModel(String dataId, InterfaceEnum interfaceEnum, String startTime) {
		String datas = AesUtilTest.getData(interfaceEnum, dataId, startTime);
		JSONArray jsonArray = JSONArray.parseArray(datas);
		JSONObject jsonRoot = jsonArray.getJSONObject(0);
		if ("成功".equals(jsonRoot.get("note"))) {
			return JsonToBean(jsonRoot, interfaceEnum);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<T> JsonToBean(JSONObject jsonRoot, InterfaceEnum interfaceEnum) {

		// 企业注册信息
		if (interfaceEnum.getName().endsWith(InterfaceEnum.EPS_REGISTER.getName())) {
			JSONObject jsonObject = (JSONObject) jsonRoot.get("result");
			JSONArray jsonData = jsonObject.getJSONArray("result");
			arrCopyFile(jsonData, interfaceEnum.getName());
			List<T> personBeanList = (List<T>) JSON.parseObject(jsonData.toJSONString(),
					(TypeReference<T>) interfaceEnum.getObj());
			return (List<T>) personBeanList;
		} else {
			if (jsonRoot.get("result") instanceof JSONArray) {
				JSONArray jsonData = jsonRoot.getJSONArray("result");
				arrCopyFile(jsonData, interfaceEnum.getName());
				if (!jsonData.isEmpty()) {
					List<T> personBeanList = (List<T>) JSON.parseObject(jsonData.toJSONString(),
							(TypeReference<T>) interfaceEnum.getObj());
					return personBeanList;
				}
			}
			if (jsonRoot.get("result") instanceof JSONObject) {
				JSONObject jsonData = jsonRoot.getJSONObject("result");
				if (!jsonData.isEmpty()) {
					objCopyFile(jsonData, interfaceEnum.getName());
					T t = (T) JSON.parseObject(jsonData.toJSONString(), interfaceEnum.getObj().getClass());
					List<T> personBeanList = new ArrayList<T>();
					personBeanList.add(t);
					return personBeanList;
				}

			}
			return null;

		}
	}

	// 复制文件
	private void arrCopyFile(JSONArray obj, String name) {
		for (int i = 0; i < obj.size() - 1; i++) {
			JSONObject object = obj.getJSONObject(i);
			objCopyFile(object, name);
		}
	}

	// 复制文件
	private void objCopyFile(JSONObject obj, String name) {
		String fileurl = obj.getString("fileurl");
		if (null != fileurl && !"".equals(fileurl)) {
			String filename = "";
			if (null != obj.getString("filename") && !"".equals("filename")) {
				filename = obj.getString("filename");
			} else if (null != obj.getString("datafilename") && !"".equals("datafilename")) {
				filename = obj.getString("datafilename");
			}
			log.info("接口名称：" + name + ",文件名称：" + filename + "，文件下载路径:" + fileurl);
			fileurl = fileurl.replace(sourceUrl, agentUrl);
			FileInfoDto dto = ConnectUtil.sendRequest(fileurl);
			// 文件服务器属性储存在接口表中的新增字段。
			if (null != dto) {
				obj.put("path", dto.getPath());
				obj.put("newfilename", dto.getFilename());
			}

		}
	}

}
